home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 272_01 / wc.c < prev    next >
Text File  |  1987-03-28  |  4KB  |  147 lines

  1. /*
  2. **                WORD COUNT UTILITY
  3. **
  4. ** syntax:
  5. ** wc [-cwlsv] file
  6. ** -c count only characters
  7. ** -w count only words
  8. ** -l count only lines
  9. ** -s gives only a hex checksum
  10. ** -q silences reporting of filenames
  11. ** file is input file (Do NOT use stdin redirection!)
  12. ** options must be first argument only
  13. ** wildcard filenames are allowed, as well as multiple filenames
  14. **
  15. ** ========== COPYRIGHT 1986 BY STEVEN E. MARGISON ==============
  16. ** 9-18-86 A  Datalight C
  17. **
  18. **   As distributed, this program requires (for compilation):
  19. **     "Steve's Datalight Library" version 1.10 or later
  20. **   which may be obtained without registration from many Bulletin
  21. **   Board Systems including:
  22. **      Compuserve IBMSW
  23. **      Cul-De-Sac (Holliston, MA.)
  24. **      HAL-PC BBS (Houston, TX.)
  25. **   and software library houses including:
  26. **      Public (Software) Library (Houston, TX.)
  27. **
  28. **   or by registration:
  29. **      $10 for Docs, Small Model Library
  30. **      $25 for Docs, S, D, L, P libraries, and complete library source
  31. **              in C and Assembler
  32. **     Steven E. Margison
  33. **     124 Sixth Street
  34. **     Downers Grove, IL, 60515
  35. **
  36. **
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <smdefs.h>
  41.  
  42. int binflg, copt, wopt, lopt, sopt, allopt, verbose;
  43. char path[64], fname[MAXFN], dest[64 + MAXFN + 1];
  44. FILE *fd;
  45.  
  46. main(argc, argv)
  47. int argc;
  48. char *argv[];
  49. {
  50.    char *cksum, *nc, *nl, *nw;
  51.    int index, c, inword, argci;
  52.  
  53.    if(argc < 2) usage();
  54.    verbose = YES;
  55.    sopt = lopt = wopt = copt = FALSE;
  56.    allopt=TRUE;
  57.    argci = 1;
  58.  
  59.    if(argv[argci][0] is '-') {
  60.       index = 0;
  61.       while(argv[argci][++index] isnot NULL) {
  62.          switch(tolower(argv[argci][index])) {
  63.             case 'q':
  64.                verbose = NO;
  65.                break;
  66.             case 'c': 
  67.                copt = TRUE;
  68.                allopt = FALSE;
  69.                break;
  70.             case 'w':
  71.                wopt = TRUE;
  72.                allopt = FALSE;
  73.                break;
  74.             case 'l':
  75.                lopt = TRUE;
  76.                allopt = FALSE;
  77.                break;
  78.             case 's':
  79.                sopt = TRUE;
  80.                allopt = FALSE;
  81.                break;
  82.             default:
  83.                usage();
  84.          } /* end of switch */
  85.       } /* end of while */
  86.    argci++;    /* get around options */
  87.    } /* end of option "if" */
  88.  
  89.    while(argci < argc) {
  90.       cksum = nc = nl = nw = 0;
  91.       inword = NO;
  92.       if(argv[argci][0] is '-') usage();
  93.  
  94.       do_open(argv[argci++]);
  95.  
  96.       while((c = fgetc(fd)) isnot EOF) {
  97.          cksum += c;
  98.          if(binflg) continue;
  99.          if(c is '\n') {
  100.             ++nl;
  101.             ++nc;
  102.             ++nc;     /* because a newline is actually two characters */
  103.             inword = NO;
  104.          }
  105.          else ++nc;
  106.          if(isspace(c)) inword = NO;
  107.          else if(inword is NO) {
  108.             inword = YES;
  109.             ++nw;
  110.             }
  111.          }  /* end of inner while */
  112.       if(allopt and !binflg)
  113.          printf("%6d characters  %d words  %d lines  %6x checksum\n",
  114.             nc, nw, nl, cksum);
  115.       if(binflg) printf("%x checksum\n", cksum);
  116.  
  117.       if(copt) printf("%d\n", nc);
  118.       if(wopt) printf("%d\n", nw);
  119.       if(lopt) printf("%d\n", nl);
  120.       if(sopt) printf("%x\n", cksum);
  121.       fclose(fd);
  122.    } /* end of file loop */
  123. }
  124.  
  125. usage() {
  126. fputs("WC Version 1.42 9-18-86  Copyright 1986 S.E. Margison\n", stderr);
  127. error("usage: wc [-cwlsq] <file1, file2, filen>");
  128. }
  129.  
  130. do_open(string) char *string; {
  131.    binflg = NO;
  132.    if(exttyp(string, "OBJ") is YES) { binflg = YES; goto AA; }
  133.    if(exttyp(string, "EXE") is YES) { binflg = YES; goto AA; }
  134.    if(exttyp(string, "COM") is YES) { binflg = YES; goto AA; }
  135.    if(exttyp(string, "ARC") is YES) { binflg = YES; goto AA; }
  136.    if(exttyp(string, "LIB") is YES) binflg = YES;
  137.    AA:
  138.    if(!binflg) {
  139.       if((fd = fopen(string, "r")) is NULL) cant(string);
  140.       }
  141.    else {
  142.       if((fd = fopen(string, "rb")) is NULL) cant(string);
  143.       }
  144.    if(verbose) printf("File: %s\n", string);
  145.    }
  146.  
  147.